<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 */
require_once ROOT_PATH . '/lib/confs/sysConf.php';
require_once ROOT_PATH . '/lib/controllers/ArgClassController.php';
require_once ROOT_PATH . '/lib/models/eimadmin/CompStruct.php';

$sysConst = new sysConf();
$locRights = $_SESSION['localRights'];

/*function getConfirmMsg($formvalues) {
	//print_r($formvalues);exit;
	$objResponse = new xajaxResponse();
	$objResponse->addAlert('ddd');
	return $objResponse->getXML();
}

$objAjax = new xajax();
$objAjax->registerFunction('getConfirmMsg');
$objAjax->processRequests();*/


//$workshifts = $records[0];
$list = $records['list'];
$employees = $records['employees'];
$year = $records['year'];
$month = $records['month'];
$employee_id = $records['employee_id'];
$rights = $records['rights'];
$pageNO = isset($_POST['pageNO'])?$_POST['pageNO']:'1';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php

//$headingInfo =$this->popArr['headinginfo'];

//$currentPage = $this->popArr['currentPage'];
//$emplist = $this->popArr['emplist'];

/*if (!isset($this->getArr['sortField']) || ($this->getArr['sortField'] == '')) {
$this->getArr['sortField']=4;
$this->getArr['sortOrder4']='ASC';
}*/

function getNextSortOrder($curSortOrder) {
	switch ($curSortOrder) {
		case 'null' :
			return 'ASC';
			break;
		case 'ASC' :
			return 'DESC';
			break;
		case 'DESC'	:
			return 'ASC';
			break;
	}

}

$GLOBALS['lang_Common_SortAscending'] = $lang_Common_SortAscending;
$GLOBALS['lang_Common_SortDescending'] = $lang_Common_SortDescending;

function nextSortOrderInWords($sortOrder) {
	return $sortOrder == 'ASC' ? $GLOBALS['lang_Common_SortDescending'] : $GLOBALS['lang_Common_SortAscending'];
}
?>
<head>
<link href="../../themes/<?php echo $styleSheet; ?>/css/style.css" rel="stylesheet" type="text/css"/>
<!--[if lte IE 6]>
<link href="../../themes/<?php echo $styleSheet; ?>/css/IE6_style.css" rel="stylesheet" type="text/css"/>
<![endif]-->
<!--[if IE]>
<link href="../../themes/<?php echo $styleSheet; ?>/css/IE_style.css" rel="stylesheet" type="text/css"/>
<![endif]-->
<script type="text/javascript" src="../../themes/<?php echo $styleSheet;?>/scripts/style.js"></script>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="../../scripts/octopus.js"></script>
<script type="text/javascript" src="../../scripts/ajax.js"></script>
<script type="text/javascript" src="../../scripts/common.js"></script>
<script type="text/javascript" src="../../scripts/jquery/jquery.js"></script>
<?php //$objAjax->printJavascript(); ?>
<script type="text/javascript">
//<![CDATA[
function nextPage() {
	i=document.standardView.pageNO.value;
	i++;
	document.standardView.pageNO.value=i;
	document.standardView.submit();
}

function prevPage() {
	var i=document.standardView.pageNO.value;
	i--;
	document.standardView.pageNO.value=i;
	document.standardView.submit();
}

function chgPage(pNO) {
	document.standardView.pageNO.value=pNO;
	document.standardView.submit();
}

function getConfirmMsg() {
	
	xajax_getConfirmMsg();
}

function sortAndSearch(sortField, sortOrder) {
	var uri = "<?php echo $_SERVER['PHP_SELF']?>?reqcode=<?php //echo $this->getArr['reqcode']?>&VIEW=MAIN&sortField=" + sortField + "&sortOrder" + sortField + "=" + sortOrder;
	document.standardView.action = uri;
	document.standardView.submit();
}

function argClass(rgtz, txt, parnt) {
	currentEditNodeValues = ['', '', '', '', ''];
	document.frmAddNode.reset();
	//document.getElementById("cmbLocation").selectedIndex = 0;
	document.getElementById("cmbType").selectedIndex = 0;
	document.frmAddNode.rgt.value=rgtz;
	document.frmAddNode.sqlState.value='NewRecord';
	document.getElementById("parnt").innerHTML="排班系统"+txt;
	document.getElementById("txtParnt").value=parnt;
	document.getElementById("layerForm").style.visibility="visible";
}

function frmAddHide () {
	//document.getElementById("txtType").style.visibility="hidden";
	document.getElementById("layerForm").style.visibility="hidden";
	//document.getElementById("layerFormLoc").style.visibility = "hidden";
	//document.getElementById("tblCompStruct").focus();
}

function frmEditHide () {
	//document.getElementById("txtType").style.visibility="hidden";
	document.getElementById("layerEditForm").style.visibility="hidden";
	//document.getElementById("layerFormLoc").style.visibility = "hidden";
}

function handle_request(tdcell,val,uid,year,month,day,group,group_object){
	//val=document.getElementById("number_"+id).value;
	url="../../ajax_handle.php?action=argclass_member&year="+year+"&month="+month+"&group="+group+"&gobject="+group_object+"&uid="+uid+"&day="+day+"&val="+val;
	//alert(url);
	//sendRequest(url,"m_"+id+"|allnumber|allmoney");
	sendRequest(url);
	document.getElementById(tdcell).value = val;
	document.getElementById('ShowDefinedClass').style.display = 'none';
	alert("排班调整成功！");
}


<?php if(false) { ?>
function returnAdd() {

	location.href = "./CentralController.php?reqcode=<?php //echo $this->getArr['reqcode']?>&capturemode=addmode";

}

function returnDelete() {
	$check = 0;
	with (document.standardView) {
		for (var i=0; i < elements.length; i++) {
			if ((elements[i].type == 'checkbox') && (elements[i].checked == true)){
				$check = 1;
			}
		}
	}

	if ( $check == 1 ){
		document.standardView.delState.value = 'DeleteMode';
		document.standardView.pageNO.value=1;
		document.standardView.submit();
	}else{
		alert("<?php echo $lang_Error_SelectAtLeastOneRecordToDelete; ?>");
	}
}

<?php } else { ?>
function returnAdd() {
	var popup=window.open('../../templates/hrfunct/emppop.php?reqcode=<?php //echo $this->getArr['reqcode']?>','Employees','height=450,width=400,scrollbars=1');
	if(!popup.opener) popup.opener=self;
}
<?php } ?>

function returnSearch() {
	document.standardView.captureState.value = 'SearchMode';
	document.standardView.pageNO.value=1;
	document.standardView.submit();
}

function hadMembersRecord() {
	var year = $('year').value;
	var month = $('month').value;
	var url="../../ajax_handle.php?action=check_batch_member&year=2013&month=1";
	//alert(url);
	var result=sendRequest(url,"batchmembers");
	return false;
}

//xajax.getFormValues(\'standardView\')   formvalues
function batchAdd() {
	document.standardView.captureState.value = 'AddMode';
	//document.standardView.pageNO.value=1;
	var url="../../ajax_handle.php";
	$.get(url, {
		action: 'check_batch_member',
		year: $('#year').val(),
		month: $('#month').val() 
	}, function(data,textStatus) {
		if(data=='yes') {
			if(confirm("该月已安排人员值班，点确定将覆盖该月原先的人员排班记录！")==false) return;
		}
		document.standardView.action = '<?php echo $_SERVER['PHP_SELF']?>?argcode=argclass&action=BatchAdd_Class_Members';
		document.standardView.submit();
	});
	//xajax_getConfirmMsg(formvalues);
	//alert('ddd');
}

function doHandleAll()
{
	with (document.standardView) {
		if(elements['allCheck'].checked == false){
			doUnCheckAll();
		}
		else if(elements['allCheck'].checked == true){
			doCheckAll();
		}
	}
}

function doCheckAll() {
	with (document.standardView) {
		for (var i=0; i < elements.length; i++) {
			if (elements[i].type == 'checkbox') {
				elements[i].checked = true;
			}
		}
	}
}

function doUnCheckAll()
{
	with (document.standardView) {
		for (var i=0; i < elements.length; i++) {
			if (elements[i].type == 'checkbox') {
				elements[i].checked = false;
			}
		}
	}
}

/**
* If at least one day is unchecked, main check box would be unchecked
*/

function unCheckMain() {
	noOfCheckboxes = 0;
	noOfCheckedCheckboxes = 0;

	with (document.getElementById('standardView')) {
		for (i = 0; i < elements.length; i++) {
			if (elements[i].type == 'checkbox' && elements[i].name != 'allCheck') {
				noOfCheckboxes++;
				if (elements[i].checked == true) {
					noOfCheckedCheckboxes++;
				}

			}
		}
	}

	document.getElementById('allCheck').checked = (noOfCheckboxes == noOfCheckedCheckboxes);
}

function clear_form() {
	document.standardView.loc_code.options[0].selected=true;
	document.standardView.loc_name.value='';
}

function actionShowAdd() {
	$('addPanel').style.display = 'block';
	$('frmAddWorkShift').reset();
}

parent.scrollTo(0, 0);
//]]>
</script>
</head>
<body>

<div class="outerbox">
	<form name="standardView" id="standardView" method="post" action="<?php echo $_SERVER['PHP_SELF']?>?argcode=argclass&action=View_Class_Members">

		<div class="mainHeading"><h2>工作排班</h2></div>
		<input type="hidden" name="captureState" value="<?php //echo isset($this->postArr['captureState'])?$this->postArr['captureState']:''?>" />
		<input type="hidden" name="delState" value="" />
		<input type="hidden" name="pageNO" value="<?php echo $pageNO;?>" />
		<!--input type="hidden" name="pageNO" value="<?php //echo isset($this->postArr['pageNO'])?$this->postArr['pageNO']:'1'?>" /-->
		<input type="hidden" name="empID" value="" />


		<?php
		if (isset($this->getArr['message'])) {
			$expString  = $this->getArr['message'];
			$messageType = CommonFunctions::getCssClassForMessage($expString);
			$messageType = 'failure';
		?>
		<div class="messagebar">
			<span class="<?php echo $messageType; ?>"><?php echo $$expString; ?></span>
		</div>
		<?php
		}
		?>

		<div class="searchbox">
			<label for="year">选择年份：</label>
			<select name="year" id="year">
				<?php
				//$optionCount = count($srchlist[0]);
				if (!isset($year)) $year = @date("Y",time());
				for ($c=2010; 2051>$c; $c++) {
					if ($year==$c) {
						echo "<option selected value='$c'>".$c ."年</option>";
					} else {
						echo "<option value='$c'>".$c ."年</option>";
					}
				}
				?>
			</select>
			<label for="month">选择月份：</label>
			<select name="month" id="month">
				<?php
				if (!isset($month)) $month = @date("m",time());
				for ($c=1; 13>$c; $c++) {
					if ($month==$c) {
						echo "<option selected value='$c'>".$c ."月</option>";
					} else {
						echo "<option value='$c'>".$c ."月</option>";
					}
				}
				?>
			</select>
			
			<label for="employee">选择员工：</label>
			<select name="employee" id="employee">
				<option value="">全部</option>
				<?php
				foreach ($employees as $employee) {
					if ($employee['id']==$employee_id) {
						echo "<option selected value='".$employee['id']."'>".$employee['name'] ."</option>";
					} else {
						echo "<option value='".$employee['id']."'>".$employee['name'] ."</option>";
					}
				}
				?>
			</select>
			
			<!--label for="loc_name"><?php //echo $description?></label>
			<input type="text" size="20" name="loc_name" id="loc_name" value="<?php //echo isset($this->postArr['loc_name'])? stripslashes($this->postArr['loc_name']):''?>" /-->
			<input type="button" class="plainbtn" onclick="returnSearch();"
				onmouseover="this.className='plainbtn plainbtnhov'" onmouseout="this.className='plainbtn'"
				value="<?php echo $lang_Common_Search;?>" />
			<input type="button" class="plainbtn" onclick="clear_form();"
				onmouseover="this.className='plainbtn plainbtnhov'" onmouseout="this.className='plainbtn'"
				 value="<?php echo $lang_Common_Reset;?>" />
			
			<br class="clear"/>
		</div>
			<div class="actionbar">
				<div class="actionbuttons">
					<?php $locRights['add']=true;?>
					<input type="button" class="plainbtn"
					<?php echo ($locRights['add']) ? 'onclick="batchAdd();"' : 'style=visibility:hidden;'; ?>
						onmouseover="this.className='plainbtn plainbtnhov'" onmouseout="this.className='plainbtn'"
						value="依据年度排班表生成选中月份的员工原始排班表" />

					<?php //if($this->getArr['reqcode']=='EMP') { ?>
					<?php if(true) { ?>
						<input type="button" class="plainbtn"
						<?php echo ($locRights['delete']) ? 'onclick="returnDelete();"' : 'style=visibility:hidden;'; ?>
							onmouseover="this.className='plainbtn plainbtnhov'" onmouseout="this.className='plainbtn'"
							value="<?php echo $lang_Common_Delete;?>" />

					<?php } ?>
				</div>
				<div class="noresultsbar"><?php //echo (empty($emplist)) ? $norecorddisplay : '';?></div>
				<div id="ShowDefinedClass" class="shownameclass" style="display:none;width:120px;left:500px;top:130px"><table id="tbclass" width="100" border="0" cellspacing="0" cellpadding="0"></table></div>
				<div class="pagingbar">
				<?php
				//$temp = $this->popArr['temp'];
				$currentPage = 1;
				$commonFunc = new CommonFunctions();
				$pageStr = $commonFunc->printPageLinks($records['Allcount'], $pageNO);
				$pageStr = preg_replace(array('/#first/', '/#previous/', '/#next/', '/#last/'), array($lang_empview_first, $lang_empview_previous, $lang_empview_next, $lang_empview_last), $pageStr);

				echo $pageStr;

				?>
				</div>
			<br class="clear" />
			</div>
			<br class="clear" />
				<table cellspacing="0" cellpadding="0" class="data-table">
					<thead>
					<tr>
						<td scope="col" width="40px;">班次</td>
						<td scope="col" width="40px;">姓名</td>
						<?php
						for ($i=1;$i<32;$i++) {
							echo "<td scope='col' style='text-align:center;'>".$i."</td>";
						}
						?>
					</tr>
					</thead>

					<tbody>
					<?php
					$n = count($list);
					$j=0;
					$getClassUrl = $_SERVER['PHP_SELF']."?argcode=argclass&action=GetClasses";
					foreach($list as $eid=>$empinfo) {
						$j++;
						$rstyle = $j%2==0 ? " class='row1'" : "";
						$inptyle = $j%2==0 ? " style='text-align:center;background-color:#ffb;'" : " style='text-align:center;'";
						echo "<tr ".$rstyle."><td>".$empinfo['group_name']."</td><td>".$empinfo['member_name']."</td>";
						$g = $empinfo['group_object'];
						
						if(in_array($month,array(1,3,5,7,8,10,12))) $max_day = 31;
						elseif(in_array($month,array(4,6,9,11))) $max_day = 30;
						elseif( $year%4==0 && ($year%100!=0 || $year%400==0) ) $max_day = 29; //闰年二月份29天
						else $max_day = 28;
						$maxd = $max_day+1;
						for ($p=1;$p<$maxd;$p++) {
							//echo "<td scope='col' align='center'><a href='#layerForm' class='add' onclick=\"argClass('', '', '', '')\">排班</a></td>";
							//dbclick='handle_request(this.value,$empinfo[mbid],$year,$month,$p,\"$empinfo[m_group]\",\"".urlencode($empinfo['group_type'])."\")'
							$paramArr = array('mid'=>$empinfo[mbid],'year'=>$year,'month'=>$month,'day'=>$p,'group'=>$empinfo[m_group],'gobject'=>$empinfo['group_object']);
							echo "<td scope='col' style='text-align:center;'><input name='m_class".$j.$p."' id='m_class".$j.$p."' value='".$empinfo['m_class'][$p]."' size=1 ".$inptyle." onfocus='GetDefinedClassList(this,\"".$getClassUrl."\",\"tbclass\",\"ShowDefinedClass\",\"m_class".$j.$p."\",\"".addslashes(serialize($paramArr))."\")' readonly/></td>";
						}
						echo "</tr>";
					}
					?>

					</tbody>
				</table>
	</form>
</div>

<!-- Add Subdivision  -->
	<div id="layerForm"  class="frame" style="visibility:hidden;margin-right:10px;width:420px;">
        <div class="outerbox">
        <div class="subHeading"><h3 id="parnt">&nbsp;</h3></div>
		<form name="frmAddNode" id="frmAddNode" method="post" action="<?php echo $_SERVER['PHP_SELF']?>?uniqcode=<?php //echo $this->getArr['uniqcode']?>&amp;id=1" onsubmit="validate(); return false;">

		<input type="hidden" value="" id="add_rgt" name="rgt"/>
		<input type="hidden" value=""  name="sqlState"/>
		<input type="hidden" value="" id="txtParnt" name="txtParnt"/>

    	<!--label id="lblDeptId" for="txtDeptId"><?php echo $lang_compstruct_Dept_Id; ?></label>
        <input type="text" value="" id="txtDeptId" name="txtDeptId" class="formInputText"/>
        <br class="clear" />

        <label id="lblSubDivision" for="txtTitle"><?php echo $lang_compstruct_Name; ?><span class="required">*</span></label>
        <input type="text" value="" id="txtTitle" name="txtTitle" class="formInputText"/>
        <br class="clear" /-->

        <label id="lblType" for="cmbType">班制选择<span class="required">*</span></label>
		<select name="cmbType" id="cmbType" class="formSelect">
            <option value="null"><?php echo $lang_Leave_Common_Select; ?></option>
			<?php foreach ($workshifts as $workshift) { 
				$value = $workshift->getName();
				$option = $workshift->getName()."-".$workshift->getTypeName();
				echo "<option value='".$value."'>".$option."</option>";
			?>
				<?php //vprintf('<option value="%s">%s</option>', $typex);?>
			<?php } ?>
		</select>
        <br class="clear" />

  		<!--label id="lblLocation" for="cmbLocation"> <?php echo $lang_compstruct_Location?></label>
        <select name="cmbLocation" id="cmbLocation" onchange="locChange(this);" class="formSelect">
            <option value=""><?php echo $lang_Leave_Common_Select; ?></option>
			<?php
			//if ($locations) {
				//foreach ($locations as $location) { ?>
			<option value="<?php //echo $location[0]; ?>"><?php //echo $location[1]; ?></option>
			<?php	//}
			  //} ?>
			<option value="Other"><?php echo $lang_compstruct_Other; ?></option>
		</select>
        <br class="clear" /-->

        <div class="formbuttons">
            <input type="button" class="savebutton"
                onclick="validate();" onmouseover="moverButton(this);" onmouseout="moutButton(this);"
                value="<?php echo $lang_Common_Save;?>" />
            <input type="button" class="clearbutton" onclick="resetAddNodeForm();"
                onmouseover="moverButton(this);" onmouseout="moutButton(this);"
                 value="<?php echo $lang_Common_Reset;?>" />
			<input type="button" class="savebutton" onclick="frmAddHide();" value="<?php echo $lang_compstruct_hide;?>" />
		</div>
	</form>
    <br class="clear" />
    </div>
    <div class="requirednotice"><?php echo preg_replace('/#star/', '<span class="error">*</span>', $lang_Commn_RequiredFieldMark); ?>.</div>
    
<script type="text/javascript">
<!--
if (document.getElementById && document.createElement) {
	roundBorder('outerbox');
}
-->
</script>
</body>
</html>
